<?php
/**
 * Created by PhpStorm.
 * User: 图图
 * Date: 2021-6-8
 * Time: 16:29
 */

namespace app\api\controller;

use app\common\controller\Api;
use app\common\model\Factor;
use app\common\model\Fenorder;
use app\common\model\Goods as GoodsModel;
use app\common\model\Store;
use Exception;
use think\Controller;
use think\Db;
use think\Request;
use app\common\controller\Unionpay;
use app\common\controller\Alipay;
use function foo\func;

/**
 * 前台首页控制器
 * @package app\index\controller
 */
class My extends Home
{
    public $noNeedLogin = [ ];


    public function home()
    {
        $token = $this->request->header('token');
        $userInfo = Db::name('qt_users')
            ->alias('u')
            ->leftJoin('qt_post p','u.post_id = p.id')
            ->leftJoin('qt_group g','u.group_id = g.id')
            ->field('u.id,u.avatar,u.nickname,u.phone,u.work_number,u.work_status,u.status,u.work_time,u.rank,g.name,p.id as post_id,p.title,p.post_step,p.post_money,p.content')
            ->where('login_token', $token)
            ->find();
        if (!empty($userInfo)) {
            $workTime = Db::name('qt_work')->where('post_id',$userInfo['post_id'])->value('time_rule');
            $userInfo['work_time_rule'] = $workTime;
            if (empty($userInfo['avatar'])) {
                $userInfo['avatar'] = config('site_url') . '/static/home/img/zcool.jpg';
            }else{
                $userInfo['avatar'] = get_img_path($userInfo['avatar']);
            }
            $this->result('请求成功', $userInfo, 200);
        } else {
            $this->result('无效的用户', '', 204);
        }

    }

    /**
     * 修改用户头像
     */
    public function edit_avatar()
    {
        if ($this->request->isPost()) {
            $userInfo = $this->userInfo;
            $data = $this->request->post();
            if (empty($data['avatar'])) {
                $this->result('请上传头像', '', 204);
            }
            $re = Db::name('qt_users')->where('id', $userInfo['id'])->update(['avatar' => $data['avatar']]);
            if ($re) {
                $this->result('修改成功', '', 200);
            } else {
                $this->result('修改失败', '', 204);
            }
        } else {
            $this->result('非法请求', '', 204);
        }
    }


    /**
     * 关于我们
     */
    public function about(){
        $data = Db::name('qt_company_config')->find();
        $data['logo'] = get_img_path($data['logo']);
        $this->result('请求成功',$data,200);
    }


    /**
     * 获取上下级部门
     */
    public function get_group(){
        if($this->request->isGet()){
            $gateData = $this->request->get();
            if(empty($gateData['type'])){
                $this->result('缺少参数','',204);
            }
            $userInfo = $this->userInfo;
            $data = $this->findGroup($userInfo['group_id'],$gateData['type']);
            $this->result('查询成功',$data,200);
        }
        $this->result('无效的请求','',204);
    }


    /**
     * 获取下级部门
     */
    public function down_group(){
        if($this->request->isGet()){
            $userInfo = $this->userInfo;
            if($userInfo['rank'] == '3'){
                $data = Db::name('qt_group')->field('id as group_id,name')->where('team_id',$userInfo['team_id'])->select();
                $this->result('查询成功',$data,200);
            }
            $data = $this->group($userInfo['group_id']);
            $this->result('查询成功',$data,200);
        }
        $this->result('无效的请求','',204);
    }


   public function getTree($list,$pid=0,$level=0) {
      static $tree = array();
      foreach($list as $row) {
             if($row['pid']==$pid) {
                    $row['level'] = $level;
              $tree[] = $row;
              $this->getTree($list, $row['id'], $level + 1);
          }
     }
     return $tree;
 }


    private function getGroup($groupId){
        static $res = array();
        $groupRes = Db::name('qt_group')->where('pid',$groupId)->select();
        if(empty($groupRes)){
            return $res;
        }else{
            foreach ($groupRes as $key=>$value){
                $res[] = $value;
                $chileGroupRes = Db::name('qt_group')->where('pid',$value['id'])->select();
                if(!empty($chileGroupRes)){
                    $this->getGroup($value['id']);
                }
            }
            return $res;
        }
    }


    /**
     * 获取上下级岗位
     */
    public function get_post(){
        if($this->request->isGet()){
            $gateData = $this->request->get();
            if(empty($gateData['type'])){
                $this->result('缺少参数','',204);
            }
            $userInfo = $this->userInfo;
            $data = $this->findPost($userInfo['post_id'],$gateData['type']);
            $this->result('查询成功',$data,200);
        }
        $this->result('无效的请求','',204);
    }


    /**
     * 获取上下级员工
     */
    public function get_user(){
        if($this->request->isGet()){
            $gateData = $this->request->get();
            if(empty($gateData['type'])){
                $this->result('缺少参数','',204);
            }
            $userInfo = $this->userInfo;
            //查找上级岗位ID
            if($gateData['type'] == '1'){
                $pidRes = $this->findPostId($userInfo['post_id'],'1');
                $userRes = Db::name('qt_users')
                    ->alias('u')
                    ->leftJoin('qt_post p','p.id = u.post_id')
                    ->field('u.id,u.nickname,p.title')
                    ->where('u.team_id',$userInfo['team_id'])
                    ->whereIn('p.id',$pidRes)
                    ->select();
            }
            //查找下级岗位ID
            if($gateData['type'] == '2'){
                $pidRes = Db::name('qt_post')->where('post_pid',$userInfo['post_id'])->column('id');
                //dump($pidRes);die;
                $userRes = Db::name('qt_users')
                    ->alias('u')
                    ->leftJoin('qt_post p','p.id = u.post_id')
                    ->field('u.id,u.nickname,p.title')
                    ->where('u.team_id',$userInfo['team_id'])
                    ->whereIn('p.id',$pidRes)
                    ->select();
            }
            $this->result('查询成功',$userRes,200);
        }
        $this->result('无效的请求','',204);
    }


    /**
     * 查找部门
     * @param $postId [当前用户ID]
     * @param $type [类型 1 向上查找 2 向下查找]
     */
    public function findPostId($postId,$type){
        //向上查找
        if($type == 1){
            static $data = [];
            if($postId == '0'){
                return $data;
            }
            $groupPid = Db::name('qt_post')->where('id',$postId)->value('post_pid as pid');
            $groupRes = Db::name('qt_post')->where('id',$groupPid)->find();
            if(empty($groupRes)){
                return $data;
            }
            $data[] = $groupRes['id'];
            if($groupRes['post_pid'] != '0'){
                $this->findPostId($groupRes['id'],'1');
            }
            return $data;
        }
        //向下查找
        if($type == 2){
            $groupRes = Db::name('qt_post')->field('id,title,post_pid as pid')->select();
            $data  = $this->get_downline($groupRes,$postId);
            $result = [];
            foreach ($data as $key=>$value){
                $result[$key] = $value['id'];
            }
            return $result;
        }
    }




    /**
     * 查找岗位
     * @param $postId [当前用户ID]
     * @param $type [类型 1 向上查找 2 向下查找]
     */
    public function findPost($postId,$type){
        //向上查找
        if($type == 1){
            static $data = [];
            if($postId == '1' || $postId == '0'){
                return $data;
            }
            $groupPid = Db::name('qt_post')->where('id',$postId)->value('post_pid as pid');
            if($groupPid == '0'){
                return $data;
            }
            $groupRes = Db::name('qt_post')->where('id',$groupPid)->find();
            $data[] = $groupRes['title'];
            if($groupRes['post_pid'] != '1'){
                $this->findGroup($groupRes['id'],'1');
            }
            return $data;
        }
        //向下查找
        if($type == 2){
            $groupRes = Db::name('qt_post')->field('id,title,post_pid as pid')->select();
            $data  = $this->get_downline($groupRes,$postId);
            $result = [];
            foreach ($data as $key=>$value){
                $result[$key] = $value['title'];
            }
            return $result;
        }
    }





    /**
     * 查找部门
     * @param $userId [当前用户ID]
     * @param $type [类型 1 向上查找 2 向下查找]
     */
    public function findGroup($groupId,$type){
        //向上查找
        if($type == 1){
            static $data = [];
            if($groupId == '1'){
                return $data;
            }
            $groupPid = Db::name('qt_group')->where('id',$groupId)->value('pid');
            $groupRes = Db::name('qt_group')->where('id',$groupPid)->find();
            $data[] = $groupRes['name'];
            if($groupRes['pid'] != '1'){
                $this->findGroup($groupRes['id'],'1');
            }
            return $data;
        }
        //向下查找
        if($type == 2){
            $groupRes = Db::name('qt_group')->where('status','1')->field('id,name,pid')->select();
            $data  = $this->get_downline($groupRes,$groupId);
            $result = [];
            foreach ($data as $key=>$value){
                $result[$key] = $value['name'];
            }
            return $result;
        }

    }


    /**
     * 查找日报部门
     * @param $userId [当前用户ID]
     * @param $type [类型 1 向上查找 2 向下查找]
     */
    public function group($groupId){
        //向上查找
        $groupRes = Db::name('qt_group')->where('status','1')->field('id,name,pid')->select();
        $data  = $this->get_downline($groupRes,$groupId);

        $result = [];
        foreach ($data as $key=>$value){
            $result[$key]['group_id'] = $value['id'];
            $result[$key]['name'] = $value['name'];
        }
        return $result;


    }



    //获取用户的所有下级ID
    function get_downline($members,$mid,$level=0){
        $arr=array();
        foreach ($members as $key => $v) {
            if($v['pid']==$mid){  //pid为0的是顶级分类
                $v['level'] = $level+1;
                $arr[]=$v;
                $arr = array_merge($arr,$this->get_downline($members,$v['id'],$level+1));
            }
        }
        return $arr;
    }

}
